package com.wswy.wzcx.widget

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.View
import android.widget.Checkable
import com.wswy.wzcx.R

class SwitchButton : View ,Checkable{

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {

    }

    private val disableColor = ContextCompat.getColor(context, R.color.textColorPrimary_20)
    private val checkedColor = ContextCompat.getColor(context, R.color.colorPrimary)

    private var currColor= disableColor

    private val rect = Rect()
    private val rectF = RectF()

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    private val outRectStrokeWidth = 5f

    private var startX  = 0f
    private var endX = 0f

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        canvas?:return

        paint.color = currColor
        paint.style  = Paint.Style.STROKE
        paint.strokeWidth = outRectStrokeWidth

        canvas.drawRoundRect(rectF,height/2f,height/2f,paint)


        val cy = height / 2f

        paint.style = Paint.Style.FILL

        if (cxOffset == 0f){
            canvas.drawCircle(outRectStrokeWidth * 2 + cRadius  ,cy,cRadius,paint)
        }else{
            canvas.drawCircle(cxOffset  ,cy,cRadius,paint)
        }

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        val oldW = measuredWidth
        val oldH = measuredHeight

        super.onMeasure(widthMeasureSpec, heightMeasureSpec)


        if (oldW != measuredWidth || oldH != measuredHeight) {

            rect.set(0, 0, measuredWidth, measuredHeight)
            rectF.set(rect)
            rectF.inset(outRectStrokeWidth / 2, outRectStrokeWidth / 2)
            cRadius = (rectF.height() - outRectStrokeWidth * 2) / 2f


            startX = outRectStrokeWidth * 2 + cRadius
            endX = rectF.right - outRectStrokeWidth * 2 - cRadius + 3



            if (checkedStatus){
                cxOffset = endX
                currColor = checkedColor
            }else{
                cxOffset = startX
                currColor = disableColor
            }
        }
    }

    private var checkedStatus = false

    private var cxOffset = 0f

    private var cRadius = 0f

    private var animPending = false

    private fun startAnim(){

        if (animPending){
            return
        }

        currColor = if (checkedStatus) checkedColor else disableColor

        val start = if (checkedStatus) startX else endX
        val end = if (checkedStatus) endX else startX

        ValueAnimator.ofFloat(start,end).run {

            addListener(object : AnimatorListenerAdapter(){
                override fun onAnimationStart(animation: Animator?) {
                    super.onAnimationStart(animation)
                    animPending = true
                }

                override fun onAnimationEnd(animation: Animator?) {
                    super.onAnimationEnd(animation)
                    animPending = false
                }
            })

            addUpdateListener {
                cxOffset = it.animatedValue as Float
                postInvalidateOnAnimation()
            }
            start()
        }

    }


    override fun isChecked(): Boolean {
        return checkedStatus
    }

    override fun toggle() {
        if (!animPending){
            isChecked = !isChecked
            startAnim()
        }
    }

    override fun setChecked(checked: Boolean) {
        checkedStatus = checked
    }

}